//@author A0096759E

package googlecal;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;

import storage.Storage;

public class GoogleCalendar{
	
	//private static final String GOOGLE_BASE_URL = "https://www.googleapis.com/calendar/v3/calendars/primary/events";
	private static String accessToken = "nil";
	private static GoogleAuthenticatorUI googleAuth;
	private static GoogleSynchronizer googleSync;
	private static Thread googleSyncThread;
	private static Storage eventsList;

	/**
	 * Takes in the current Storage element, and starts the Google Authentication Process.
	 * @param storage - the storage element of LifeShelf
	 */
	public static void initGoogleSync(Storage storage) {
			//Open GoogleAuth 
			googleAuth = new GoogleAuthenticatorUI();
			//Set Window Location
			Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
			int width = (int)screenSize.getWidth();
			int height = (int)screenSize.getHeight();
			googleAuth.initAuth(new Point(width-550,height-550));
			eventsList = storage;
	}
	
	/**
	 * Takes in the validated access token from the GoogleAuthenticatorUI class, and starts GoogleSynchronizer
	 * in a new thread, passing in the access token and Storage element declared by the constructor. 
	 * @param accessTokenFromGAuth
	 */
	public static void startGoogleSync(String accessTokenFromGAuth) {
		accessToken = accessTokenFromGAuth;
		googleSyncThread = new Thread(setGoogleSync(new GoogleSynchronizer(accessToken, eventsList)));
		googleSyncThread.start();
	}

	/**
	 * Returns the GoogleSynchronizer object that is running
	 * @return googleSync
	 */
	public static GoogleSynchronizer getGoogleSync() {
		return googleSync;
	}
	
	/**
	 * Sets the googleSync field to the GoogleSynchronizer object that is running, and returns it.
	 * @param googleSync
	 * @return googleSync
	 */
	public static GoogleSynchronizer setGoogleSync(GoogleSynchronizer googleSync) {
		GoogleCalendar.googleSync = googleSync;
		return googleSync;
	}
}